<template>
    <view :style="objColor">
		<view v-if="sellerNo == 'jw'">
			<view class="wrap" >
			    <image src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/register_bg1.png" mode ="widthFix"  class="bg_image"></image>
			    <view class="main">
			        <text class="amount">{{ rewardBalance }}
					<!-- <text style="font-size:100rpx">￥</text> -->
					</text>
			    </view>
			   <button class="btn_login" type="warn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
			        <text class="tx">立即领取</text>
			    </button>
				
				<view class="tips">*活动仅限广州、深圳、北京地区</view>
				
			</view>
		</view>
		<view v-else>
			<view class="wrap" >
			    <image src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/register_bg.png" mode ="widthFix" class="bg_image"></image>
			    <view class="main">
			        <text class="amount">{{ rewardBalance }}</text>
			        <image class="yuan" src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/yuan.png"></image>
			    </view>
			
			    <button class="btn_login" type="warn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
			        <text class="tx">授权登录</text>
			        <image class="img" src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/right.png"></image>
			    </button>
			</view>
			
			<!-- 未授权 -->
			<!-- <view class="maskbg" v-if="!hasUserInfo">
			    <view class="maskinfo">
			        <view class="masktxt">无法获取您的用户信息，请点击授权用户信息</view>
			        <view class="maskbtn" @tap="getUserProfile">确定</view>
			    </view>
			</view> -->
		</view>
    </view>
</template>

<script>
'use strict';

// exports.__esModule = true; // pages/registration/registration.js

var app = getApp();
var newapp = getApp();

var https_1 = require('./../../../api/https.js');
export default {
    data() {
        return {
            hasUserInfo: false,
            tips: '提示',
            sellerNo: '',
            rewardBalance: 0,
            userInfo: {},
            openId: '',
            unionId: '',
            session_key: '',
            memberId: '',
			sellerNo:app.globalData.sellerNo
			
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        this.login(); // newapp.CHECK_LOGIN(() => {
        // })

        this.getReward(); // this.getTokenInfo()
		console.log(options.memberId,'options.memberId')
            this.sellerNo=app.globalData.sellerNo,
            this.memberId=options.memberId
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getUserProfile();
    },
    methods: {
        // 登录函数
        login: function () {
            var that = this;
            uni.login({
				provider: 'weixin',
                success: function (res) {
                    if (res.code) {
                        https_1
                            .getJsCode2session({
                                sellerId: app.globalData.sellerId,
                                jsCode: res.code
                            })
                            .then(function (res) {
                                if (res.code == 0) {
                                    app.globalData.openId = res.data.openid;
                                    app.globalData.unionId = res.data.unionid;
                                    app.globalData.session_key = res.data.session_key;
                                    uni.setStorageSync('openid', res.data.openid);
                                    uni.setStorageSync('unionId', res.data.unionid);
                                    uni.setStorageSync('session_key', res.data.session_key);
                                    that.openId=res.data.openid,
                                        that.session_key=res.data.session_key,
                                        that.unionId=res.data.unionid
                                } else {
                                    uni.showModal({
                                        title: '消息提示',
                                        content: '获取配置信息失败，请联系商家管理员！',
                                        confirmText: '确定',
                                        showCancel: false
                                    });
	                                if(getCurrentPages()[getCurrentPages().length - 1].route !== 'pages/errorPage/index'){
		                                uni.navigateTo({
			                                url: '/pages/errorPage/index?code=500'
		                                });
	                                }
                                }
                            });
                    }
                }
            });
        },

        // 获取手机号
        getPhoneNumber: function (e) {
            var that = this;
            var tmp = this;

            if (e.detail.errMsg == 'getPhoneNumber:ok' && e.detail.encryptedData != '') {
                //获取手机号成功  // 调用登录接口
                https_1
                    .loginCheck({
                        openId: tmp.openId,
                        unionId: tmp.unionId,
                        phoneKey: tmp.session_key,
                        encryptedData: e.detail.encryptedData,
                        iv: e.detail.iv,
                        sellerNo: app.globalData.sellerNo,
                        type: '3'
                    })
                    .then(function (res) {
                        if (res.code == 0) {
                            //手机号已注册过 重新获取token登录
                            try {
                                uni.setStorageSync('tel', res.msg);
                                uni.setStorageSync('token', res.data);
                                uni.setStorageSync('newPeople', false);
                                uni.setStorageSync('openId', tmp.openId);
                                https_1
                                    .citareaListByNamey({
                                        name: uni.getStorageSync('cityName')
                                    })
                                    .then(function (res) {
                                        if (res.code == 0) {
                                            uni.setStorageSync('cityId', res.data.id);
                                            uni.setStorageSync('currentCityId', res.data.id);
                                        }
                                    }); // wx.navigateBack()
                                // 返回首页

                                uni.switchTab({
                                    url: '../../../pages/index/index'
                                });
                            } catch (error) {
                                // console.log('error :>> ', error);
                            }
                        } else {
                            //手机号未注册
                            https_1
                                .login({
                                    openId: tmp.openId,
                                    unionId: tmp.unionId,
                                    type: '3',
                                    name: '微信用户',
                                    icon: 'https://oos-cn.ctyunapi.cn/front-end/static/static/photo.png',
                                    phoneKey: tmp.session_key,
                                    encryptedData: e.detail.encryptedData,
                                    iv: e.detail.iv,
                                    sellerNo: app.globalData.sellerNo,
                                    inviterMemberId: tmp.memberId //邀请人Id
                                })
                                .then(function (res) {
                                    if (res.code == 0 || res.code == 2) {
                                        //注册成功
                                        uni.setStorageSync('token', res.data);
                                        uni.setStorageSync('tel', res.msg);
                                        uni.setStorageSync('newPeople', true);
                                        https_1
                                            .citareaListByNamey({
                                                name: uni.getStorageSync('cityName')
                                            })
                                            .then(function (res) {
                                                if (res.code == 0) {
                                                    uni.setStorageSync('cityId', res.data.id);
                                                    uni.setStorageSync('currentCityId', res.data.id);
                                                }
                                            }); // wx.navigateBack()
                                        // 返回首页

                                        uni.switchTab({
                                            url: '../../../pages/index/index'
                                        });
                                    } else {
                                        // 绑定失败
                                        uni.showToast({
                                            title: res.msg,
                                            icon: 'none',
                                            duration: 2000
                                        });
                                    }
                                });
                        }
                    });
            } else {
                uni.showToast({
                    title: '获取手机号失败',
                    icon: 'none',
                    duration: 2000
                });
            }
        },

        // 获取个人信息
        getUserProfile: function () {
            var that = this; // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
            // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗

            uni.getUserProfile({
                desc: '用于完善会员资料',
                success: function (res) {
                    that.userInfo=res.userInfo,
                        that.hasUserInfo=true
                }
            });
        },

        // 登录成功后台  需要token向服务端发送昵称和头像
        updateMemberInfo: function (icon, name) {
            app.globalData.wxRequestPost(
                'updateMemberInfo',
                {
                    icon: icon,
                    name: name
                },
                function (res) {
                    if (res.data.code == 0) {
                        // 返回首页
                        uni.switchTab({
                            url: '/packageFunctional/sitelist/sitelist'
                        });
                    } else {
                        uni.showToast({
                            title: '存入头像昵称失败',
                            icon: 'none',
                            duration: 2000,
                            success: function (res) {                 
                                // 返回首页
                                uni.switchTab({
                                    url: '/packageFunctional/sitelist/sitelist'
                                });
                            }
                        });  
                    }      
                }
            );
        },

        // 获取后台活动金额
        getReward: function () {
            var that = this;
            https_1
                .getOldAndNewBenifit2({
					sellerId: newapp.globalData.sellerId   
				})
                .then(function (res) {     
                    if (res.code == 0) {
                        var rewardBalance = res.data.rewardBalance / 100;
                        that.rewardBalance=rewardBalance // 好友注册
                    }
                })
                ['catch'](function (err) {
                    console.log(err);
                });
        }
    }
};
</script>
<style lang="less">
/* pages/registration/registration.less */
@import "./registration.less";
/* .wrap .main {
    height: 240rpx;
    margin: 33vh auto 0;
    display: flex;
    align-items: baseline;
    justify-content: center;
}
.wrap .main .amount {
    font-size: 180rpx;
    font-weight: bold;
    color: #fb0c02;
}
.wrap .main .yuan {
    width: 50rpx;
    height: 50rpx;
}
.wrap .btn_login {
    width: 467rpx;
    height: 92rpx;
    margin: 20vh auto 0;
    background: linear-gradient(0deg, #ffc05c 0%, #ff9c00 99%);
    border-radius: 46rpx;
    display: flex;
    align-items: center;
    justify-content: center;
}
.wrap .btn_login .img {
    width: 48rpx;
    height: 48rpx;
    margin: 0 0 0 10rpx;
}
.maskbg {
    width: 100vw;
    height: 65vh;
    padding-top: 35vh;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 10;
    background: rgba(0, 0, 0, 0.5);
}
.maskbg .maskinfo {
    width: 60vw;
    margin-left: 12vw;
    background: #fff;
    padding: 5vw 8vw;
    border: #eee solid 1rpx;
    border-radius: 5rpx;
}
.maskbg .maskinfo .masktxt {
    text-align: left;
    color: #666;
    letter-spacing: 1px;
    font-size: 34rpx;
}
.maskbg .maskinfo .maskbtn {
    text-align: right;
    color: #049f1e;
    letter-spacing: 1px;
    height: 60rpx;
    font-size: 34rpx;
} */
</style>
